﻿/******************************************************    
文件名:TableXContentDB.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问XContent数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    /// <summary>
    /// 某牌号的包含不同元素的含量
    /// </summary>
    class TableXContentDB
    {
        operate op = new operate();

        public void UpdateXcontent(DataTable dt,int table_type)
        {
            //清空现有数据
            clearAllXContent(table_type);
            List<string> lx = new List<string>();
            lx = getX(dt, table_type);
            Xcontent xc = new Xcontent();
            int sid = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sid = GetSID(dt.Rows[i][0].ToString(), table_type);
                for (int j = 0; j < lx.Count; j++)
                {
                    xc = new Xcontent();
                    xc.s_id = sid;
                    xc.table_type = table_type;
                    xc.x = lx[j];
                    xc.x_content = dt.Rows[i][xc.x].ToString();
                    if (!string.IsNullOrEmpty(xc.x_content.Trim()))
                    {
                        insertx(xc);
                    }
                }
            }
        }

        /// <summary>
        /// 获取材料牌号id
        /// </summary>
        /// <param name="name"></param>
        /// <param name="table_type"></param>
        /// <returns></returns>
        private int GetSID(string name,int table_type)
        {
            string sql = string.Empty;
            string table=string.Empty;
            int sid = 0;
            if(table_type==1)
            {
                table="steel_composition";
            }
            else
            {
                table="materials_number";
            }
            sql = "select id from " + table + " where name='" + name + "'";
            DataTable dt = new DataTable();
            dt = op.Query(sql);
            if (dt == null || dt.Rows.Count == 0)
            {
                sid = 0;
            }
            else
            {
                sid = Convert.ToInt32(dt.Rows[0][0]);
            }
            return sid;
        }

        private List<string> getX(DataTable dt,int table_type)
        {
            List<string> col=new List<string>();
            int start = 0;
            if (table_type == 1)
            {
                start = 4;
            }
            else
            {
                start = 3;
            }
            int end = dt.Columns.Count - 1;
            for (int i = start; i < end; i++)
            {
                string cn = dt.Columns[i].ColumnName;
                col.Add(cn);
            }
            return col;
        }

        /// <summary>
        /// 清空某类型表的所有x数据
        /// </summary>
        /// <param name="table_type"></param>
        /// <returns></returns>
        public bool clearAllXContent(int table_type)
        {
            string sql = @"DELETE FROM x_content WHERE table_type ='" + table_type + "'";
            return op.Execute(sql);
        }

        /// <summary>
        /// 删除重复记录
        /// </summary>
        /// <param name="s_id"></param>
        /// <param name="table_type"></param>
        /// <returns></returns>
        private bool deleteItem(int s_id, int table_type)
        {
            string sql = @"DELETE FROM x_content WHERE s_id='" + s_id.ToString() + "' and table_type ='" + table_type + "'";
            return op.Execute(sql);
        }

        /// <summary>
        /// 元素类定义
        /// </summary>
        private class Xcontent
        {
            public int s_id, table_type;
            public string x, x_content;
        }

        /// <summary>
        /// 插入一条元素记录
        /// </summary>
        /// <param name="xc"></param>
        /// <returns></returns>
        private bool insertx(Xcontent xc)
        {
            string sql = @"INSERT INTO x_content
             (s_id,x,x_content,table_type) VALUES
             ('" + xc.s_id.ToString() + "','" + xc.x + "','" + xc.x_content + "','" + xc.table_type.ToString() + "')";
            return op.Execute(sql);
        }
    }
}
